#include <QtGui>
#include "QtStra_InstEdit.h"



QtStraInstEdit::QtStraInstEdit(QTableWidget& s, QWidget * parent): QDialog(parent)
{
	m_parent = static_cast<QtStraTemplate*>(parent);

	m_table = &s;

	//insert list
	{
		m_list = new QListWidget;

		m_list->setSortingEnabled(true);

		m_list->setAlternatingRowColors(true);




		typedef map<string, vector<InstInfo> >::iterator it_mappdt;
		for(it_mappdt itp = CStrategySys::m_instRead.begin();itp != CStrategySys::m_instRead.end(); ++itp)
		{
			vector<InstInfo>& vInst = itp->second;

			typedef vector<InstInfo>::iterator it_vInst;
			for (it_vInst vit = vInst.begin(); vit != vInst.end(); vit++)
			{
				QString qstrInst = vit->inst;

				if (false == ExistInTable(qstrInst))
				{
					QListWidgetItem *item = new QListWidgetItem(qstrInst, m_list);
				}

				
			}

		}

	}



	m_left = new QPushButton(tr(">>"));
	m_right = new QPushButton(tr("<<"));
	m_okbtn = new QPushButton(tr("OK"));
	connect(m_left, SIGNAL(clicked()), this, SLOT(OnLeftBtnClicked()));
	connect(m_right, SIGNAL(clicked()), this, SLOT(OnRightBtnClicked()));
	connect(m_okbtn, SIGNAL(clicked()), this, SLOT(OnOKBtnClicked()));



	QVBoxLayout* midlayout = new QVBoxLayout;
	midlayout->addStretch();
	midlayout->addWidget(m_left);
	midlayout->addWidget(m_right);
	midlayout->addStretch();


	QHBoxLayout* topLayout = new QHBoxLayout; 
	topLayout->addWidget(m_list);
	topLayout->addLayout(midlayout);
	topLayout->addWidget(m_table);


	QHBoxLayout* bottomLayout = new QHBoxLayout; 
	bottomLayout->addStretch();
	bottomLayout->addWidget(m_okbtn);
	bottomLayout->addStretch();


	QVBoxLayout* mainlayout = new QVBoxLayout;
	mainlayout->addLayout(topLayout);
	mainlayout->addLayout(bottomLayout);


	setLayout(mainlayout);

	setWindowTitle(tr("Edit"));
}

bool QtStraInstEdit::ExistInTable(QString qstrInst)
{
	int rowcnt = m_table->rowCount();

	int colcnt = m_table->columnCount();


	for (int rowIndex = 0; rowIndex < rowcnt; rowIndex++)
	{
		vector < pair<char*, char*> > attriRow;


		QString strvalue = m_table->item(rowIndex, 0)->text();


		if (qstrInst == strvalue)
		{
			return true;
		}
		
	}

	return false;

}



void QtStraInstEdit::OnOKBtnClicked()
{

	accept();
}

void QtStraInstEdit::OnLeftBtnClicked()
{
	int listrowIndex = m_list->currentRow();

	//focus on row
	if (-1 != listrowIndex)
	{

		//get inst from list
		QString Inst = m_list->item(listrowIndex)->text();



				
		//insert row to table
		int tablerowcnt = m_table->rowCount();

		m_table->insertRow(tablerowcnt);

		int colcnt = m_table->columnCount();

		for (int colIndex = 0; colIndex < colcnt; colIndex++)
		{
			QTableWidgetItem *item = NULL;

			if (0 == colIndex)
			{
				item = new QTableWidgetItem(Inst);

				item->setCheckState(Qt::Unchecked);
			}
			else
			{
				item = new QTableWidgetItem(m_parent->DefaultInstTableValue(colIndex));
			}

			item->setTextAlignment(Qt::AlignCenter);

			m_table->setItem(tablerowcnt, colIndex, item);
		}


		//remove row
		QListWidgetItem* listItem = m_list->takeItem(listrowIndex);

		if (listItem)
			delete listItem;

	}
}

void QtStraInstEdit::OnRightBtnClicked()
{
	int talberowIndex = m_table->currentRow();
	
	//focus on row
	if (-1 != talberowIndex)
	{
		QString Inst = m_table->item(talberowIndex, 0)->text();


		m_list->insertItem(0, Inst);

		m_table->removeRow(talberowIndex);
	
	}

}